package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.admin.service.HospitalTypeService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.HospitalType;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.HospitalTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class HospitalTypeServiceImpl extends ServiceImpl<HospitalTypeMapper, HospitalType> implements HospitalTypeService {
    private HospitalTypeMapper hospitalTypeMapper;

    @Override
    public RestResult all(ListDto listDto) {
        HospitalType hospitalType = MapUtil.conversionMapToObj((Map) listDto.getObj(), HospitalType.class);
        IPage ipage = new Page(listDto.getPage(),listDto.getPageSize());
        QueryWrapper<HospitalType> queryWrapper = new QueryWrapper<HospitalType>()
                .like(hospitalType.getName() != null,"name",hospitalType.getName())
                .eq(hospitalType.getHospitalTypeId() != null,"hospital_type_id",hospitalType.getHospitalTypeId());

        IPage hospitalPate = hospitalTypeMapper.selectPage(ipage,queryWrapper);
        return new RestResultBuilder().success(ListVo
                .builder()
                .list(hospitalPate.getRecords())
                .total(hospitalPate.getTotal())
                .build()
        );
    }

    @Override
    public RestResult allId(HospitalType hospitalType) {
        QueryWrapper<HospitalType> queryWrapper = new QueryWrapper<HospitalType>()
                .select("hospital_type_id", "name")
                .like(hospitalType.getName() != null, "name", hospitalType.getName());
        return new RestResultBuilder().success(hospitalTypeMapper.selectList(queryWrapper));
    }
}
